/**
 * @project heima-leadnews
 * @author MECHREVO
 * @date 2024/1/15 09:30:39
 * @version 1.0
 */
package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.Thread.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Service
@Transactional
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //1.参数校验
        if(multipartFile==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.将前端传过来的图片上传到minio
        String originalFilename = multipartFile.getOriginalFilename();
        int indexOf = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(indexOf);
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        String imageUrl="";
        try {
             imageUrl = fileStorageService.uploadImgFile("", name + suffix, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 3.将minio返回的图片的ur1封装成wmMaterial对象，并存入wm_materia1表中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtils.getUser().getId());
        wmMaterial.setUrl(imageUrl);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        // 4.将wmMateria1对象返回给前端
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //检查参数
        if(dto==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        IPage<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.lambdaQuery(WmMaterial.class)
                .eq(StringUtils.isNotBlank(dto.getIsCollection()) && "1".equals(dto.getIsCollection()),
                WmMaterial::getIsCollection, dto.getIsCollection())
                .eq(WmMaterial::getUserId,WmThreadLocalUtils.getUser().getId())
                .orderByDesc(WmMaterial::getCreatedTime);
        //Page<WmMaterial> selectPage = wmMaterialMapper.selectPage(page);
        page(page,wrapper);
        //5.封装结果返回
        PageResponseResult pageResponseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.ok(AppHttpCodeEnum.SUCCESS.getCode(),page.getRecords(),AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return pageResponseResult;
    }

    @Override
    public ResponseResult collect(Long id) {
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        wmMaterial.setIsCollection((short)1);
        return ResponseResult.okResult(wmMaterialMapper.updateById(wmMaterial));
    }

    @Override
    public ResponseResult AncelCollect(Long id) {
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        wmMaterial.setIsCollection((short)0);
        return ResponseResult.okResult(wmMaterialMapper.updateById(wmMaterial));
    }

    @Override
    public ResponseResult updatePicture(Long id) {
        return ResponseResult.okResult(wmMaterialMapper.deleteById(id));
    }
}
